package com.example.mobie2032021606170hsy0617homework;

import android.content.ContentValues;
import android.content.DialogInterface;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Telephony;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btMainAdd;
    private RecyclerView rvMainMassage;
    private List<Staff> list = new ArrayList<>();
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        openHelper = new MySQLiteOpenHelper(this, "Database.db",
                null, 1);
        database = openHelper.getWritableDatabase();
        Cursor cursor = database.query("add", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String skill = cursor.getString(cursor.getColumnIndex("skill"));
                int money = cursor.getInt(cursor.getColumnIndex("money"));
                list.add(new Staff(id, name, skill, money));
            } while (cursor.moveToFirst());
        }
        cursor.close();
        database.close();
        rvMainMassage.setLayoutManager(new LinearLayoutManager(this));
        rvMainMassage.setAdapter(new StaffRVAdapter(list));
        btMainAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final  View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.add,null);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("添加数据")
                        .setView(view)
                        .setPositiveButton("添加", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = ((EditText) view.findViewById(R.id.edit_name))
                                        .getText().toString().trim();
                                String skill = ((EditText) view.findViewById(R.id.edit_skill))
                                        .getText().toString().trim();
                                int money = Integer.parseInt(((EditText)view.findViewById(R.id.edit_money)).getText().toString().trim());
                                ContentValues values = new ContentValues();
                                values.put("name", name);
                                values.put("skill", skill);
                                values.put("money", money);
                                database = openHelper.getWritableDatabase();
                                database.insert("add", null, values);
                                list.clear();
                                Cursor cursor = database.query("add", null, null, null, null, null, null);
                                if (cursor.moveToFirst()) {
                                    do {
                                        int id = cursor.getInt(cursor.getColumnIndex("_id"));
                                        name = cursor.getString(cursor.getColumnIndex("name"));
                                        skill = cursor.getString(cursor.getColumnIndex("skill"));
                                        money = cursor.getInt(cursor.getColumnIndex("money"));
                                        list.add(new Staff(id, name, skill, money));
                                    } while (cursor.moveToFirst());
                                }
                                rvMainMassage.getAdapter().notifyDataSetChanged();
                                cursor.close();
                                database.close();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        btMainAdd = (Button) findViewById(R.id.btMainAdd);
        rvMainMassage = (RecyclerView) findViewById(R.id.rvMainMassage);
    }
}